// component/toast/toast.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    toastType: { 
      type: Number, 
      value: 0,
    },
    message: {
      type: String,
      value: '',
      observer: function (newVal, oldVal) {
        if (newVal && newVal != '') {
          this.show();
        }
      }
    },
    modalOptions: {
      type: Object,
      value: null,
      observer: function (newVal, oldVal) {
        if (newVal) {
          this.modalShow();
        }
      }
    },
    float: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show: function() {
      clearTimeout(this.cancelTimeout);
      this.setData({
        isShow: true,
      })
      this.cancelTimeout = setTimeout(() => {
        this.hide();
      }, 2000);
    },
    hide: function() {
      this.setData({
        message: '',
        isShow: false
      })
    },
    modalShow: function() {
      clearTimeout(this.cancelTimeout);
      this.setData({
        isShow: true,
      })
    },
    modalHide: function() {
      this.setData({
        modalOptions: null,
        isShow: false,
      })
    },
    cancel: function() {
      this.triggerEvent('cancel', {}, {});
      this.modalHide();
    },
    confirm: function() {
      var data = {};
      if (this.data.modalOptions.id) {
        data.toastid = this.data.modalOptions.id;
      }
      this.triggerEvent('confirm', data, {});
      this.modalHide();
    },
  }
})
